package com.youngball.business.utill;

import com.youngball.business.exception.MyDefineException;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.List;

/**
 * Created by Administrator on 2015/5/7.
 */
public class StringUtils {

    public static Integer getRadio(String radion, double prices) {
        String[] stringKinds = radion.split("/");
        int rodio = (int) prices / Integer.parseInt(stringKinds[0]);
        return rodio * Integer.parseInt(stringKinds[1]);
    }

    public static int exist(Object[] objects, Object ob) {
        for (int i = 0; i < objects.length; i++) {
            try {
                if (objects[i].getClass().equals(ob)||objects[i].equals(ob)) {
                    return i;
                }

            } catch (Exception e) {
                return -1;
            }
        }
        return -1;
    }

    public static int exist(List objects, Object ob) {
        for (int i = 0; i < objects.size(); i++) {
            try {
                if (objects.get(i).getClass().equals(ob)||objects.get(i).equals(ob)) {
                    return i;
                }

            } catch (Exception e) {
                return -1;
            }
        }
        return -1;

    }

    public  static String getEnumToClass(String value){
        String [] values=value.split("_");
        String myString="";
        for (int i=0;i<values.length;i++){
            if (i==values.length-1){
                myString=myString+values[i];
            }else {
                myString=values[i]+"_";
            }
        }
        return myString;
    }


    public  static String getClassToEnum(String value){
        String [] values=value.split(":");
        String myString="";
        for (int i=0;i<values.length;i++){
            if (i==values.length-1){
                myString=myString+values[i];
            }else {
                myString=myString+values[i]+"_";
            }

        }
        return myString;
    }

    public static String getMD5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            return new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            throw new MyDefineException("MD5加密出现错误");
        }
    }
}
